#coding=utf-8
from driver import Proxy
import random  # 取随机数
import re
from db.mysql_conn import MysqlUtils, config
mysql = MysqlUtils(config)
from config.conf import proxiesDEF

get_goods_num = 0
goodsArr = []

def getAsin():
    try:
        goodsArr = mysql.query('select id, dp, star, reviews, url, name, img, country from newbook where price = 0 and id > 60000 limit 400')
    except:
        goodsArr = []

    proxies_list = ['121.227.43.60:9999', '113.103.122.23:61234', '14.20.235.230:9797', '47.107.134.197:8000', '180.117.131.118:8118', '181.214.240.157:3129', '119.178.101.18:8888', '47.90.95.31:8118', '219.159.38.199:56210', '181.214.240.74:3129', '219.159.38.207:56210', '60.211.218.78:53281', '124.93.201.59:42672', '218.22.7.62:53281', '1.197.16.155:9999', '122.136.212.132:53281', '222.240.184.126:8086', '119.57.108.53:53281', '123.206.60.123:8118', '203.110.164.139:52144', '61.164.39.66:53281', '118.78.196.194:8118', '47.110.130.152:80', '112.195.81.13:8118', '1.198.72.193:9999', '61.164.39.67:53281', '115.28.148.192:8118', '59.125.31.116:45965', '47.95.249.140:8118', '211.152.33.24:59523', '119.120.76.61:61234', '122.4.41.53:61234', '181.214.240.126:3129', '181.214.240.25:3129', '221.122.91.60:80', '121.79.131.58:8080', '222.223.182.66:8000', '221.122.91.61:80', '221.122.91.34:80', '181.214.240.40:3129', '181.214.240.105:3129', '181.214.240.180:3129', '181.214.240.150:3129', '181.214.240.53:3129', '181.214.240.39:3129', '181.214.240.244:3129', '181.214.240.130:3129', '163.204.243.105:9999', '121.69.10.62:9090', '181.214.240.183:3129']
    # boo = [True, False, True, False, False, False, False, False, False]
    boo = [True]
    try:
        if random.choice(boo):
            proxies_list = proxiesDEF()
            print(proxies_list)
    except:
        print('获取ip失败')

    ip = proxies_list[random.randint(0, len(proxies_list)-1)]
    driver = Proxy(ip).getdriver()

    if len(goodsArr) > 0:
        getGoodsDeDetail(driver, goodsArr)
    else:
        driver.quit()
        print('=========over==============')


def getGoodsDeDetail (driver, goodsArr):
    global get_goods_num
    for index, goods in enumerate(goodsArr):
        print('==%s==================%s=================================' % (goods['id'], get_goods_num))
        get_goods_num = get_goods_num + 1
        dp = goods['dp']
        url = 'https://www.amazon.com/dp/%s' % dp
        # url = 'https://www.amazon.com/dp/0684841460'
        # url = 'https://www.amazon.com/dp/B001CB34KY'
        print(url)
        try:
            driver.get(url)
            driver.implicitly_wait(random.uniform(0.3, 0.8))
            try:
                name = driver.find_element_by_xpath('//*[@id="productTitle"]').text
            except:
                name = "xxxxxxxxx"
            try:
                resArr = driver.find_element_by_xpath('//li[@id="SalesRank"]').text
                restr = re.compile('Rank: #(.*) \(See Top')
                rankarr = restr.findall(resArr)[0].split(' in ')
                ranking = rankarr[0].replace(',', '')
                rankingtype = rankarr[1]
            except:
                ranking = -1
                rankingtype = ''
            try:
                ISBN10 = driver.find_element_by_xpath('//div/span[contains(text(), "ISBN-10:")]/following-sibling::span[1]').text
            except:
                ISBN10 = ''
            try:
                ISBN13 = driver.find_element_by_xpath('//div/span[contains(text(), "ISBN-13:")]/following-sibling::span[1]').text
            except:
                ISBN13 = ''
            try:
                Paperback = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../following-sibling::div[1]/span').text.strip()
                PaperbackDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../../../../a').get_attribute('href')
                PaperbackDP = ruturnDP(dp, PaperbackDP)
            except:
                Paperback = ''
                PaperbackDP = ''
            try:
                Hardcover = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Hardcover")]/../following-sibling::div[1]/span').text.strip()
                HardcoverDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Hardcover")]/../../../../a').get_attribute('href')
                HardcoverDP = ruturnDP(dp, HardcoverDP)
            except:
                Hardcover = ''
                HardcoverDP = ''
            try:
                Kindle = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Kindle")]/../following-sibling::div[1]/span').text.strip()
                KindleDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Kindle")]/../../../../a').get_attribute('href')
                KindleDP = ruturnDP(dp, KindleDP)
            except:
                Kindle = ''
                KindleDP = ''
            try:
                Audiobook = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audiobook")]/../following-sibling::div[1]/span').text.strip()
                AudiobookDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audiobook")]/../../../../a').get_attribute('href')
                AudiobookDP = ruturnDP(dp, AudiobookDP)
            except:
                Audiobook = ''
                AudiobookDP = ''
            try:
                AudioCD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audio CD")]/../following-sibling::div[1]/span').text.strip()
                AudioCDDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audio CD")]/../../../../a').get_attribute('href')
                AudioCDDP = ruturnDP(dp, AudioCDDP)
            except:
                AudioCD = ''
                AudioCDDP = ''
            try:
                MP3CD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "MP3 CD")]/../following-sibling::div[1]/span').text.strip()
                MP3CDDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "MP3 CD")]/../../../../a').get_attribute('href')
                MP3CDDP = ruturnDP(dp, MP3CDDP)
            except:
                MP3CD = ''
                MP3CD = ''
            try:
                SpiralBound = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Spiral-bound")]/../following-sibling::div[1]/span').text.strip()
                SpiralBoundDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Spiral-bound")]/../../../../a').get_attribute('href')
                SpiralBoundDP = ruturnDP(dp, SpiralBoundDP)
            except:
                SpiralBound = ''
                SpiralBoundDP = ''
            try:
                eTextbook = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "eTextbook")]/../following-sibling::div[1]/span').text.strip()
                eTextbookDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../../../../a').get_attribute('href')
                eTextbookDP = ruturnDP(dp, eTextbookDP)
            except:
                eTextbook = ''
                eTextbookDP = ''
            try:
                LooseLeaf = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Loose Leaf")]/../following-sibling::div[1]/span').text.strip()
                LooseLeafDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Loose Leaf")]/../../../../a').get_attribute('href')
                LooseLeafDP = ruturnDP(dp, LooseLeafDP)
            except:
                LooseLeaf = ''
                LooseLeafDP = ''
            try:
                PlasticComb = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Plastic Comb")]/../following-sibling::div[1]/span').text.strip()
                PlasticCombDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Plastic Comb")]/../../../../a').get_attribute('href')
                PlasticCombDP = ruturnDP(dp, PlasticCombDP)
            except:
                PlasticComb = ''
                PlasticCombDP = ''
            try:
                PrintedAccessCode = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Printed Access Code")]/../following-sibling::div[1]/span').text.strip()
                PrintedAccessCodeDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Printed Access Code")]/../../../../a').get_attribute('href')
                PrintedAccessCodeDP = ruturnDP(dp, PrintedAccessCodeDP)
            except:
                PrintedAccessCode = ''
                PrintedAccessCodeDP = ''
            try:
                UnknownBinding = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Unknown Binding")]/../following-sibling::div[1]/span').text.strip()
                UnknownBindingDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Unknown Binding")]/../../../../a').get_attribute('href')
                UnknownBindingDP = ruturnDP(dp, UnknownBindingDP)
            except:
                UnknownBinding = ''
                UnknownBindingDP = ''
            try:
                BookSupplement = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Book Supplement")]/../following-sibling::div[1]/span').text.strip()
                BookSupplementDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Book Supplement")]/../../../../a').get_attribute('href')
                BookSupplementDP = ruturnDP(dp, BookSupplementDP)
            except:
                BookSupplement = ''
                BookSupplementDP = ''
            try:
                MultimediaCD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Multimedia CD")]/../following-sibling::div[1]/span').text.strip()
                MultimediaCDDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Multimedia CD")]/../../../../a').get_attribute('href')
                MultimediaCDDP = ruturnDP(dp, MultimediaCDDP)
            except:
                MultimediaCD = ''
                MultimediaCDDP = ''
            try:
                Ringbound = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Ring-bound")]/../following-sibling::div[1]/span').text.strip()
                RingboundDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Ring-bound")]/../../../../a').get_attribute('href')
                RingboundDP = ruturnDP(dp, RingboundDP)
            except:
                Ringbound = ''
                RingboundDP = ''
            try:
                Pamphlet = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Pamphlet")]/../following-sibling::div[1]/span').text.strip()
                PamphletDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Pamphlet")]/../../../../a').get_attribute('href')
                PamphletDP = ruturnDP(dp, PamphletDP)
            except:
                Pamphlet = ''
                PamphletDP = ''
            try:
                LibraryBinding = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Library Binding")]/../following-sibling::div[1]/span').text.strip()
                LibraryBindingDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Library Binding")]/../../../../a').get_attribute('href')
                LibraryBindingDP = ruturnDP(dp, LibraryBindingDP)
            except:
                LibraryBinding = ''
                LibraryBindingDP = ''
            try:
                DVDROM = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD-ROM")]/../following-sibling::div[1]/span').text.strip()
                DVDROMDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD-ROM")]/../../../../a').get_attribute('href')
                DVDROMDP = ruturnDP(dp, DVDROMDP)
            except:
                DVDROM = ''
                DVDROMDP = ''
            try:
                DVD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD")]/../following-sibling::div[1]/span').text.strip()
                DVDDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD")]/../../../../a').get_attribute('href')
                DVDDP = ruturnDP(dp, DVDDP)
            except:
                DVD = ''
                DVDDP = ''
            try:
                TextbookBinding = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Textbook Binding")]/../following-sibling::div[1]/span').text.strip()
                TextbookBindingDP = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Textbook Binding")]/../../../../a').get_attribute('href')
                TextbookBindingDP = ruturnDP(dp, TextbookBindingDP)
            except:
                TextbookBinding = ''
                TextbookBindingDP = ''


            info = Paperback != '' or Hardcover != '' or Kindle != '' or AudioCD != '' or MP3CD != '' or Audiobook != '' or SpiralBound != '' or eTextbook != '' or LooseLeaf != '' or PlasticComb != '' or PrintedAccessCode != '' or UnknownBinding != '' or BookSupplement != '' or MultimediaCD != '' or Ringbound != '' or Pamphlet != '' or LibraryBinding != '' or DVDROM != '' or DVD != '' or TextbookBinding != ''
            if info:
                print('sssssssssssssssssssssssssssssssssssss')
            else:
                print('empty =========empty===========empty============empty===========================')
                try:
                    Paperback = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Paperback")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    PaperbackDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Paperback")]/../../a').get_attribute('href')
                    PaperbackDP = ruturnDP(dp, PaperbackDP)
                    print('PaperbackDP====  %s' % PaperbackDP)
                except:
                    Paperback = ''
                    PaperbackDP = ''
                try:
                    Hardcover = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Hardcover")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    HardcoverDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Hardcover")]/../../a').get_attribute('href')
                    HardcoverDP = ruturnDP(dp, HardcoverDP)
                    print('HardcoverDP  %s' % HardcoverDP)
                except:
                    Hardcover = ''
                    HardcoverDP = ''
                try:
                    Kindle = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Kindle")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    KindleDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Kindle")]/../../a').get_attribute('href')
                    KindleDP = ruturnDP(dp, KindleDP)
                    print('KindleDP  %s' % KindleDP)
                except:
                    Kindle = ''
                    KindleDP = ''
                try:
                    Audiobook = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audiobook")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    AudiobookDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audiobook")]/../../a').get_attribute('href')
                    AudiobookDP = ruturnDP(dp, AudiobookDP)
                    print('AudiobookDP  %s' % AudiobookDP)
                except:
                    Audiobook = ''
                    AudiobookDP = ''
                try:
                    AudioCD = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audio CD")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    AudioCDDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Audio CD")]/../../a').get_attribute('href')
                    AudioCDDP = ruturnDP(dp, AudioCDDP)
                    print('AudioCDDP  %s' % AudioCDDP)
                except:
                    AudioCD = ''
                    AudioCDDP = ''
                try:
                    MP3CD = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "MP3 CD")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    MP3CDDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "MP3 CD")]/../../a').get_attribute('href')
                    MP3CDDP = ruturnDP(dp, MP3CDDP)
                    print('MP3CDDP  %s' % MP3CDDP)
                except:
                    MP3CD = ''
                    MP3CDDP = ''
                try:
                    SpiralBound = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Spiral-bound")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    SpiralBoundDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Spiral-bound")]/../../a').get_attribute('href')
                    SpiralBoundDP = ruturnDP(dp, SpiralBoundDP)
                    print('SpiralBoundDP  %s' % SpiralBoundDP)
                except:
                    SpiralBound = ''
                    SpiralBoundDP = ''
                try:
                    eTextbook = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "eTextbook")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    eTextbookDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "eTextbook")]/../../a').get_attribute('href')
                    eTextbookDP = ruturnDP(dp, eTextbookDP)
                    print('eTextbookDP  %s' % eTextbookDP)
                except:
                    eTextbook = ''
                    eTextbookDP = ''
                try:
                    LooseLeaf = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Loose Leaf")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    LooseLeafDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Loose Leaf")]/../../a').get_attribute('href')
                    LooseLeafDP = ruturnDP(dp, LooseLeafDP)
                    print('LooseLeafDP  %s' % LooseLeafDP)
                except:
                    LooseLeaf = ''
                    LooseLeafDP = ''
                try:
                    PlasticComb = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Plastic Comb")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    PlasticCombDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Plastic Comb")]/../../a').get_attribute('href')
                    PlasticCombDP = ruturnDP(dp, PlasticCombDP)
                    print('PlasticCombDP  %s' % PlasticCombDP)
                except:
                    PlasticComb = ''
                    PlasticCombDP = ''
                try:
                    PrintedAccessCode = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Printed Access Code")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    PrintedAccessCodeDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Printed Access Code")]/../../a').get_attribute('href')
                    PrintedAccessCodeDP = ruturnDP(dp, PrintedAccessCodeDP)
                    print('PrintedAccessCodeDP  %s' % PrintedAccessCodeDP)
                except:
                    PrintedAccessCode = ''
                    PrintedAccessCodeDP = ''
                try:
                    UnknownBinding = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Unknown Binding")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    UnknownBindingDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Unknown Binding")]/../../a').get_attribute('href')
                    UnknownBindingDP = ruturnDP(dp, UnknownBindingDP)
                    print('UnknownBindingDP  %s' % UnknownBindingDP)
                except:
                    UnknownBinding = ''
                    UnknownBindingDP = ''
                try:
                    BookSupplement = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Book Supplement")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    BookSupplementDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Book Supplement")]/../../a').get_attribute('href')
                    BookSupplementDP = ruturnDP(dp, BookSupplementDP)
                    print('BookSupplementDP  %s' % BookSupplementDP)
                except:
                    BookSupplement = ''
                    BookSupplementDP = ''
                try:
                    MultimediaCD = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Multimedia CD")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    MultimediaCDDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Multimedia CD")]/../../a').get_attribute('href')
                    MultimediaCDDP = ruturnDP(dp, MultimediaCDDP)
                    print('MultimediaCDDP  %s' % MultimediaCDDP)
                except:
                    MultimediaCD = ''
                    MultimediaCDDP = ''
                try:
                    Ringbound = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Ring-bound")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    RingboundDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Ring-bound")]/../../a').get_attribute('href')
                    RingboundDP = ruturnDP(dp, RingboundDP)
                    print('RingboundDP  %s' % RingboundDP)
                except:
                    Ringbound = ''
                    RingboundDP = ''
                try:
                    Pamphlet = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Pamphlet")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    PamphletDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Pamphlet")]/../../a').get_attribute('href')
                    PamphletDP = ruturnDP(dp, PamphletDP)
                    print('PamphletDP  %s' % PamphletDP)
                except:
                    Pamphlet = ''
                    PamphletDP = ''
                try:
                    LibraryBinding = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Library Binding")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    LibraryBindingDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Library Binding")]/../../a').get_attribute('href')
                    LibraryBindingDP = ruturnDP(dp, LibraryBindingDP)
                    print('LibraryBindingDP  %s' % LibraryBindingDP)
                except:
                    LibraryBinding = ''
                    LibraryBindingDP = ''
                try:
                    DVDROM = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD-ROM")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    DVDROMDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD-ROM")]/../../a').get_attribute('href')
                    DVDROMDP = ruturnDP(dp, DVDROMDP)
                    print('DVDROMDP  %s' % DVDROMDP)
                except:
                    DVDROM = ''
                    DVDROMDP = ''
                try:
                    DVD = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    DVDDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "DVD")]/../../a').get_attribute('href')
                    DVDDP = ruturnDP(dp, DVDDP)
                    print('DVDDP  %s' % DVDDP)
                except:
                    DVD = ''
                    DVDDP = ''
                try:
                    TextbookBinding = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Textbook Binding")]/following-sibling::span[1]/span').get_attribute('innerHTML').strip()
                    TextbookBindingDP = driver.find_element_by_xpath('//*[@class="a-button-inner"]/a/span[contains(text(), "Textbook Binding")]/../../a').get_attribute('href')
                    TextbookBindingDP = ruturnDP(dp, TextbookBindingDP)
                    print('TextbookBindingDP  %s' % TextbookBindingDP)
                except:
                    TextbookBinding = ''
                    TextbookBindingDP = ''


            goods['ranking'] = ranking
            goods['rankingtype'] = rankingtype
            # goods['ISBN10'] = ISBN10
            # goods['ISBN13'] = ISBN13
            # goods['asin'] = goods['dp']

            print(name)
            print(ranking)
            print(rankingtype)

            print('Paperback ====   %s   ==' % Paperback)
            print('Hardcover ====   %s   ==' % Hardcover)
            print('Kindle =======   %s   ==' % Kindle)
            print('AudioCD ======   %s   ==' % AudioCD)
            print('MP3CD ======   %s   ==' % MP3CD)
            print('Audiobook ====   %s   ==' % Audiobook)
            print('SpiralBound ====   %s   ==' % SpiralBound)
            print('eTextbook ====   %s   ==' % eTextbook)
            print('Loose Leaf ====   %s   ==' % LooseLeaf)
            print('PlasticComb ====   %s   ==' % PlasticComb)
            print('PrintedAccessCode ====   %s   ==' % PrintedAccessCode)
            print('UnknownBinding ====   %s   ==' % UnknownBinding)
            print('BookSupplement ====   %s   ==' % BookSupplement)
            print('MultimediaCD ====   %s   ==' % MultimediaCD)
            print('Ringbound ====   %s   ==' % Ringbound)
            print('Pamphlet ====   %s   ==' % Pamphlet)
            print('LibraryBinding ====   %s   ==' % LibraryBinding)
            print('DVDROM ====   %s   ==' % DVDROM)
            print('DVD ====   %s   ==' % DVD)
            print('TextbookBinding ====   %s   ==' % TextbookBinding)

            sqlStr = "INSERT INTO newbook (dp,packagetype,packagelink,ISBN10,ISBN13,asin,rankingtype,ranking,star,reviews,moneytype,price,url,name,img,country ) values (%s, %s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s)"

            if Paperback != '':
                try:
                    goods['packagetype'] = 'Paperback'
                    Paperback = Paperback.replace('from', '').strip()
                    moneytype = Paperback[0:1]
                    goods['ISBN10'] = PaperbackDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Paperback.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert paperback')
                except:
                    print('Paperback=== error')
            if Hardcover != '':
                try:
                    goods['packagetype'] = 'Hardcover'
                    Hardcover = Hardcover.replace('from', '').strip()
                    moneytype = Hardcover[0:1]
                    goods['ISBN10'] = HardcoverDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Hardcover.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Hardcover')
                except:
                    print('Hardcover=== error')
            if Kindle != '':
                try:
                    goods['packagetype'] = 'Kindle'
                    Kindle = Kindle.replace('from', '').strip()
                    moneytype = Kindle[0:1]
                    goods['ISBN10'] = KindleDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Kindle.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Kindle')
                except Exception as exc:
                    print(exc)
                    print('Kindle=== error')

            if AudioCD != '':
                try:
                    goods['packagetype'] = 'AudioCD'
                    AudioCD = AudioCD.replace('from', '').strip()
                    moneytype = AudioCD[0:1]
                    goods['ISBN10'] = AudioCDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(AudioCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert AudioCD')
                except:
                    print('AudioCD=== error')
            if MP3CD != '':
                try:
                    goods['packagetype'] = 'MP3CD'
                    MP3CD = MP3CD.replace('from', '').strip()
                    moneytype = MP3CD[0:1]
                    goods['ISBN10'] = MP3CDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MP3CD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MP3CD')
                except:
                    print('MP3CD=== error')
            if Audiobook != '':
                try:
                    goods['packagetype'] = 'Audiobook'
                    Audiobook = Audiobook.replace('from', '').strip()
                    moneytype = Audiobook[0:1]
                    goods['ISBN10'] = AudiobookDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Audiobook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Audiobook')
                except:
                    print('Audiobook=== error')

            if SpiralBound != '':
                try:
                    goods['packagetype'] = 'SpiralBound'
                    SpiralBound = SpiralBound.replace('from', '').strip()
                    moneytype = SpiralBound[0:1]
                    goods['ISBN10'] = SpiralBoundDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(SpiralBound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert SpiralBound')
                except:
                    print('SpiralBound=== error')
            if eTextbook != '':
                try:
                    goods['packagetype'] = 'eTextbook'
                    eTextbook = eTextbook.replace('from', '').strip()
                    moneytype = eTextbook[0:1]
                    goods['ISBN10'] = eTextbookDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(eTextbook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert eTextbook')
                except:
                    print('eTextbook=== error')
            if LooseLeaf != '':
                try:
                    goods['packagetype'] = 'LooseLeaf'
                    LooseLeaf = LooseLeaf.replace('from', '').strip()
                    moneytype = LooseLeaf[0:1]
                    goods['ISBN10'] = LooseLeafDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LooseLeaf.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LooseLeaf')
                except:
                    print('LooseLeaf=== error')
            if PlasticComb != '':
                try:
                    goods['packagetype'] = 'PlasticComb'
                    PlasticComb = PlasticComb.replace('from', '').strip()
                    moneytype = PlasticComb[0:1]
                    goods['ISBN10'] = PlasticCombDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PlasticComb.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PlasticComb')
                except:
                    print('PlasticComb=== error')
            if PrintedAccessCode != '':
                try:
                    goods['packagetype'] = 'PrintedAccessCode'
                    PrintedAccessCode = PrintedAccessCode.replace('from', '').strip()
                    moneytype = PrintedAccessCode[0:1]
                    goods['ISBN10'] = PrintedAccessCodeDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PrintedAccessCode.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PrintedAccessCode')
                except:
                    print('PrintedAccessCode=== error')
            if UnknownBinding != '':
                try:
                    goods['packagetype'] = 'UnknownBinding'
                    UnknownBinding = UnknownBinding.replace('from', '').strip()
                    moneytype = UnknownBinding[0:1]
                    goods['ISBN10'] = UnknownBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(UnknownBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert UnknownBinding')
                except:
                    print('UnknownBinding=== error')
            if BookSupplement != '':
                try:
                    goods['packagetype'] = 'BookSupplement'
                    BookSupplement = BookSupplement.replace('from', '').strip()
                    moneytype = BookSupplement[0:1]
                    goods['ISBN10'] = BookSupplementDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(BookSupplement.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert BookSupplement')
                except:
                    print('BookSupplement=== error')
            if MultimediaCD != '':
                try:
                    goods['packagetype'] = 'MultimediaCD'
                    MultimediaCD = MultimediaCD.replace('from', '').strip()
                    moneytype = MultimediaCD[0:1]
                    goods['ISBN10'] = MultimediaCDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MultimediaCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MultimediaCD')
                except:
                    print('MultimediaCD=== error')
            if Ringbound != '':
                try:
                    goods['packagetype'] = 'Ringbound'
                    Ringbound = Ringbound.replace('from', '').strip()
                    moneytype = Ringbound[0:1]
                    goods['ISBN10'] = RingboundDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Ringbound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Ringbound')
                except:
                    print('Ringbound=== error')
            if Pamphlet != '':
                try:
                    goods['packagetype'] = 'Pamphlet'
                    Pamphlet = Pamphlet.replace('from', '').strip()
                    moneytype = Pamphlet[0:1]
                    goods['ISBN10'] = PamphletDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Pamphlet.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Pamphlet')
                except:
                    print('Pamphlet=== error')
            if LibraryBinding != '':
                try:
                    goods['packagetype'] = 'LibraryBinding'
                    LibraryBinding = LibraryBinding.replace('from', '').strip()
                    moneytype = LibraryBinding[0:1]
                    goods['ISBN10'] = LibraryBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LibraryBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LibraryBinding')
                except:
                    print('LibraryBinding=== error')
            if DVDROM != '':
                try:
                    goods['packagetype'] = 'DVDROM'
                    DVDROM = DVDROM.replace('from', '').strip()
                    moneytype = DVDROM[0:1]
                    goods['ISBN10'] = DVDROMDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVDROM.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVDROM')
                except:
                    print('DVDROM=== error')
            if DVD != '':
                try:
                    goods['packagetype'] = 'DVD'
                    DVD = DVD.replace('from', '').strip()
                    moneytype = DVD[0:1]
                    goods['ISBN10'] = DVDDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVD')
                except:
                    print('DVD=== error')
            if TextbookBinding != '':
                try:
                    goods['packagetype'] = 'TextbookBinding'
                    TextbookBinding = TextbookBinding.replace('from', '').strip()
                    moneytype = TextbookBinding[0:1]
                    goods['ISBN10'] = TextbookBindingDP
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(TextbookBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert TextbookBinding')
                except:
                    print('TextbookBinding=== error')

            info1 = Paperback != '' or Hardcover != '' or Kindle != '' or AudioCD != '' or MP3CD != '' or Audiobook != '' or SpiralBound != '' or eTextbook != '' or LooseLeaf != '' or PlasticComb != '' or PrintedAccessCode != '' or UnknownBinding != '' or BookSupplement != '' or MultimediaCD != '' or Ringbound != '' or Pamphlet != '' or LibraryBinding != '' or DVDROM != '' or DVD != '' or TextbookBinding != ''

            if info1:
                try:
                    print('deletesql')
                    deletesql = "DELETE FROM newbook WHERE id =  %s" % (goods['id'])
                    mysql.delete(deletesql)
                except:
                    print('deletesql   === error')

            # 清空数组 goods
            print('*goods***********************************')
            goods.clear()
            print(goods)

        except Exception as exc:
            print(exc)
            print('get 请求失败 跳往下一个')
    driver.quit()
    print('休息一会==========马上开始')
    getAsin()


def updateSql(goods):
    d = {
        'packagetype': goods['packagetype'],
        'ISBN10': goods['ISBN10'],
        'ISBN13': goods['ISBN13'],
        'asin': goods['asin'],
        'rankingtype': goods['rankingtype'],
        'ranking': goods['ranking'],
        'moneytype': goods['moneytype'],
        'price': goods['price'],
         }  # 更新的字段及对应值
    t = ('id', goods['id'])  # 更新所需条件
    table_name = 'newbook'
    updateSqlStr = 'UPDATE %s SET ' % table_name + ','.join(['%s=%r' % (k, d[k]) for k in d]) + ',updateTime = NOW() WHERE %s=%r;' % (t[0], t[1])
    mysql.update(updateSqlStr)


def returnGoods(goods):
    sqlParam = [
        goods['dp'],
        goods['packagetype'],
        '',
        goods['ISBN10'],
        '',
        '',
        goods['rankingtype'],
        goods['ranking'],
        goods['star'],
        goods['reviews'],
        goods['moneytype'],
        goods['price'],
        goods['url'],
        goods['name'],
        goods['img'],
        goods['country']
    ]
    return sqlParam

def averageNum(str):
    try:
        if ' - ' in str:
            arr = str.split(' - ')
            add = float(arr[0]) + float(arr[1])
            price = '%.2f' % float(add/2)
        else:
            price = '%.2f' % float(str)
    except:
        price = -1
    return price


def ruturnDP(_dp, href):
    if href == 'javascript:void(0)':
        return _dp
    dp = href.split('dp/')[1].split('/ref')[0]
    return dp


if __name__ == '__main__':
    getAsin()




